﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;

namespace CompleteReader.Dialogs
{
    public class DelayedModalDialog : UserControl
    {
        protected async void DelayShowing()
        {
            this.Opacity = 0;
            await Task.Delay(1400);
            Storyboard fadeIn = Fade(this, null, 1, TimeSpan.FromSeconds(0.5), new QuadraticEase { EasingMode = EasingMode.EaseIn });
            fadeIn.Begin();
        }

        /// <summary>
        /// Creates a basic storyboard. Each object can only have one opacity animation of with the same target opacity
        /// (due to naming conflicts)
        /// </summary>
        private Storyboard Fade(FrameworkElement _target, Double? _from, Double _to, TimeSpan _duration, EasingFunctionBase _easing)
        {
            Storyboard sb = new Storyboard();                                                               // Create a storyboard
            String ID = _target.Name.ToString();
            _target.Resources.Add(ID + "OpacityAnimation" + _to, sb);

            _target.RenderTransform = new CompositeTransform();

            DoubleAnimation opacity = new DoubleAnimation
            {
                From = _from,
                To = _to,
                Duration = new Duration(_duration),
                RepeatBehavior = new RepeatBehavior(1),
                EasingFunction = _easing
            };

            Storyboard.SetTarget(opacity, _target);
            Storyboard.SetTargetName(opacity, _target.Name);
            Storyboard.SetTargetProperty(opacity, "(UIElement.Opacity)");

            sb.Children.Add(opacity);

            return sb;
        }

    }
}
